package mdt.action;

import mdt.program.Displayer;
import mdt.gui.EdgeArrowAttributePanel;
import org.tigris.gef.base.Editor;
import org.tigris.gef.base.Globals;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

/**
 * Created by lambert on 2016/7/23.
 */
public class SetEdgeArrowAttrAction extends AbstractAction {

	public SetEdgeArrowAttrAction() {
		super("Set Edge Arrow Attributes");
		putValue(Action.SMALL_ICON, new ImageIcon(FontBoldAction.class.getResource("/resources/SetFigAttr.png")));
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		JDialog dialog = new AttrDialog("Set Edge Shapes");
		dialog.pack();
		Point frameLocation = Displayer.instance().getJFrame().getLocation();
		Rectangle selBounds = Globals.curEditor().getSelectionManager().getBounds();
		final Editor ce = Globals.curEditor();
		JComponent drawingPanel = (JPanel) ce.getJComponent();
		Component awtComp = drawingPanel;
		while (!(awtComp instanceof RootPaneContainer) && awtComp != null) {
			awtComp = awtComp.getParent();
		}
		JLayeredPane layeredPane = ((RootPaneContainer) awtComp).getLayeredPane();
		selBounds = SwingUtilities.convertRectangle(drawingPanel, selBounds, layeredPane);
		dialog.setLocation(frameLocation.x + selBounds.x, frameLocation.y + selBounds.y);
		dialog.setVisible(true);
	}

	public class AttrDialog extends JDialog {
		public AttrDialog(String title) {
			super(Displayer.instance().getJFrame(), ModalityType.DOCUMENT_MODAL);
			setTitle(title);
			Container container = getContentPane();
			container.setLayout(new BorderLayout());
			container.add(new EdgeArrowAttributePanel(), BorderLayout.CENTER);

			JButton okButton = new JButton(new AbstractAction("OK") {

				@Override
				public void actionPerformed(ActionEvent e) {
					dispose();
				}
			});

			JPanel buttonPanel = new JPanel();
			buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
			buttonPanel.add(Box.createHorizontalGlue());
			buttonPanel.add(okButton);
			buttonPanel.add(Box.createHorizontalStrut(30));
			buttonPanel.setBorder(BorderFactory.createEmptyBorder(5, 0, 15, 0));
			container.add(buttonPanel, BorderLayout.SOUTH);
		}
	}
}